package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cargo/factory")
@Slf4j
public class FactoryController extends BaseController {
    @Reference
    private FactoryService factoryService;

    private FactoryExample factoryExample;

    /**
     * 查询厂家列表
     */
    @RequestMapping("/list")
    public String list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize) {
        try {
            log.info("执行部门分页的方法");

            // 企业id，从当前登陆用户哪里获取（先写死）
            String companyId = getLoginCompanyId();
            // 调用service查询
            PageInfo<Factory> pageInfo = factoryService.findByPage(factoryExample, pageNum, pageSize);
            // 存储查询结果
            request.setAttribute("pageInfo", pageInfo);

            log.info("执行部门分页的方法 结束...");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("执行部门分页方法出现异常...",e);
        }
        return "cargo/factory/factory-list";
    }

    /**
     * 进入添加页面
     * http://localhost:8080/cargo/factory/toAdd
     */
    @RequestMapping("/toAdd")
    public String toAdd() {
        // 查询所有厂家
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);
        return "cargo/factory/factory-add";
    }

    /**
     * 添加厂家、修改厂家
     */
    @RequestMapping("/edit")
    public String edit(Factory factory) {
        // 判断
        if (StringUtils.isEmpty(factory.getId())) {
            factory.setCreateTime(new Date());
            factory.setUpdateTime(new Date());
            factoryService.save(factory);
        } else {
            factory.setUpdateTime(new Date());
            factoryService.update(factory);
        }
        return "redirect:/cargo/factory/list";
    }

    /**
     * 进入修改页面
     * 1. 根据id查询
     * 2. 查询所有厂家
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        // 根据id查询
        Factory factory = factoryService.findById(id);

        // 查询所有部门
        List<Factory> factoryList = factoryService.findAll(factoryExample);

        // 保存
        request.setAttribute("factory", factory);
        request.setAttribute("factoryList", factoryList);
        return "cargo/factory/factory-update";
    }

    /**
     * 删除厂家
     * 返回数据：{"message" : 1}
     */
    @RequestMapping("/delete")
    public String delete(String id) {
        log.info("执行购销合同删除.....");
        factoryService.delete(id);
        return "redirect:/cargo/factory/list";
    }
}
